package com.example.fw;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class HibernateHelper extends HelperBase {

	public HibernateHelper(ApplicationManager manager) {
	  super(manager);
	}

	public void deleteUser(User user) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		try {
			session.createQuery("delete User where login = :login").setString("login", user.login).executeUpdate();
		} finally {
          trans.commit();
		}
	}
	
	public boolean userExist(User user) {
		boolean userExist = false;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		try {
			if (session.load(User.class, user.login) != null)
				userExist = true;
		} finally {
          trans.commit();
		}
		return userExist;
	}

	public String getUserId(String login) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		return session.createQuery("select id from User where login =?").setParameter(0, login).uniqueResult().toString();
	}
}
